package coco.component
{
	import coco.blog.UserBlog;
	import coco.util.Utils;
	import coco.util.WindowUtil;
	
	import cocoBlog.data.BlogDirectMessage;
	import cocoBlog.data.BlogResult;
	import cocoBlog.data.BlogStatus;
	import cocoBlog.data.BlogUser;
	import cocoBlog.event.BlogErrorEvent;
	import cocoBlog.event.BlogResultEvent;
	
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.core.IVisualElement;
	import mx.core.IVisualElementContainer;
	
	import spark.components.Group;
	
	public class CocoGroup extends Group
	{
		/**此条微博是否是自己发的 Default : false*/
		private var isOwner:Boolean = false;
		
		
		private var _data:BlogStatus;

		public function get data():BlogStatus
		{
			return _data;
		}

		public function set data(value:BlogStatus):void
		{
			_data = value;
			
			if(UserBlog.getInstance().user.id == _data.user.id)  // 控件正在加载时  用户登出报错
				isOwner = true;
			
			initMenuItems();
		}
		
		
		private var _messageData:BlogDirectMessage;

		/** 私信数据*/
		public function get messageData():BlogDirectMessage
		{
			return _messageData;
		}

		/**
		 * @private
		 */
		public function set messageData(value:BlogDirectMessage):void
		{
			_messageData = value;
			
			initMenuItems();
		}
		
		
		private var _userData:BlogUser;

		/**
		 * 用户数据
		 * */
		public function get userData():BlogUser
		{
			return _userData;
		}

		/**
		 * @private
		 */
		public function set userData(value:BlogUser):void
		{
			_userData = value;
			
			if(_userData.id == UserBlog.getInstance().user.id)
				isOwner = true;
		}
		
		
		/**
		 * Menu Items
		 * */
		private var newMessage:ContextMenuItem = new ContextMenuItem('发布'); // 发布自己的微博
		private var del:ContextMenuItem = new ContextMenuItem('删除', true);  // 删除自己的微博消息
		private var repost:ContextMenuItem = new ContextMenuItem('转发', true); // 转播一条微博
		private var addFavorite:ContextMenuItem = new ContextMenuItem('收藏');    // 收藏一条微博
		private var removeFavorite:ContextMenuItem = new ContextMenuItem('取消收藏'); // 取消收藏一条微博
		private var comment:ContextMenuItem = new ContextMenuItem('评论');     //评论一条微博
		private var replyDirect:ContextMenuItem = new ContextMenuItem('回复私信'); // 回复一条私信
		private var seeDirect:ContextMenuItem = new ContextMenuItem('查看私信'); // 查看私信
		private var delDirect:ContextMenuItem = new ContextMenuItem('删除私信'); // 删除私信
		
		
		public function  CocoGroup()
		{
			newMessage.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, newMessageHandler, false, 0, true);
			del.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, delHandler, false, 0, true);
			repost.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, repostHandler, false, 0, true);
			addFavorite.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, addFavoriteHandler, false, 0, true);
			removeFavorite.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, removeFavoriteHandler, false, 0, true);
			comment.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, commentHandler, false, 0, true);
			replyDirect.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, replyDirectHandler, false, 0, true);
			seeDirect.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, seeDirectHandler, false, 0, true);
			delDirect.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, delDirectHandler, false, 0, true);
		}
		
		/**此条微博是否已经被收藏 Default: false*/
		public var isMain:Boolean = false;
		public var isFavorited:Boolean = false;  
		public var isGetDirect:Boolean = false;   // 收件箱控件
		public var isSendDirect:Boolean = false; // 发件箱的控件
		public var isSearchUser:Boolean = false; // 搜索用户显示的控件
		public var isUser:Boolean = false;
		/**
		 * Init Context Menu 
		 * */
		private function initMenuItems():void
		{
			var cm:ContextMenu = new ContextMenu();
			if(!isGetDirect && !isSendDirect)  // 发布 ：除新消息之外
				cm.customItems.push(newMessage); 
			
			if( isOwner && (isUser || isMain))   //  删除　:　自己
				cm.customItems.push(del);   
			
			if(isMain || isFavorited || isUser)
				cm.customItems.push(repost);
			
			if(isMain || isUser)  // 收藏控件
				cm.customItems.push(addFavorite);
			
			if(isFavorited)  // 移除收藏
				cm.customItems.push(removeFavorite);
			
			if(isMain || isFavorited || isUser)  // 评论
				cm.customItems.push(comment);
			
			if(isGetDirect)  //  回复私信
				cm.customItems.push(replyDirect);
			
			if(isSendDirect || isGetDirect)  // 查看私信
				cm.customItems.push(seeDirect);
			
			if(isSendDirect || isGetDirect)  // 删除私信
				cm.customItems.push(delDirect);
			
			contextMenu = cm;
		}
		
		
		/**
		 * Handlers 
		 * */
		private function replyDirectHandler(e:ContextMenuEvent):void
		{
			if(isGetDirect)
				WindowUtil.getInstance().openDirectWindow(messageData.sender);
			if(isSendDirect)
				WindowUtil.getInstance().openDirectWindow(messageData.recipient);
		}
		
		
		protected function seeDirectHandler(e:ContextMenuEvent):void
		{
			if(isGetDirect)
				WindowUtil.getInstance().openMessageWindow(messageData, 'getDirect');
			if(isSendDirect)
				WindowUtil.getInstance().openMessageWindow(messageData, 'sendDirect');
		}
		
		
		private function delDirectHandler(e:ContextMenuEvent):void
		{
			deleteDirectMessage(messageData.id);
		}
		
		
		/**
		 * 删除私信
		 * */
		private function deleteDirectMessage(id:Number):void
		{
			UserBlog.getInstance().delete_Direct_Message(id);
			UserBlog.getInstance().addEventListener(BlogResultEvent.DELETE_DIRECT_MESSAGE_RESULT, deleteDirectResult, false, 0, true);
			UserBlog.getInstance().addEventListener(BlogErrorEvent.DELETE_DIRECT_MESSAGE_ERROR, deleteDirectError, false, 0, true);
		}
		
		
		private function deleteDirectResult(e:BlogResultEvent):void
		{
			IVisualElementContainer(parent).removeElement(this);
			UserBlog.getInstance().removeEventListener(BlogResultEvent.DELETE_DIRECT_MESSAGE_RESULT, deleteDirectResult);
			UserBlog.getInstance().removeEventListener(BlogErrorEvent.DELETE_DIRECT_MESSAGE_ERROR, deleteDirectError);
		}
		
		
		private function deleteDirectError(e:BlogErrorEvent):void
		{
			UserBlog.getInstance().removeEventListener(BlogResultEvent.DELETE_DIRECT_MESSAGE_RESULT, deleteDirectResult);
			UserBlog.getInstance().removeEventListener(BlogErrorEvent.DELETE_DIRECT_MESSAGE_ERROR, deleteDirectError);
		}
		
		
		/**send message*/
		private function newMessageHandler(e:ContextMenuEvent):void
		{
			WindowUtil.getInstance().openSendMsgWindow();
		}
		
		
		private function addFavoriteHandler(e:ContextMenuEvent):void
		{
			UserBlog.getInstance().create_Favorites(_data.id);
		}
		
		
		/**
		 * Cancel Favorite
		 * */
		private function removeFavoriteHandler(e:ContextMenuEvent):void
		{
			UserBlog.getInstance().delete_Favorites(_data.id);
			UserBlog.getInstance().addEventListener(BlogResultEvent.DELETE_FAVORITES_RESULT, deleteFavoriteResult, false, 0, true);
			UserBlog.getInstance().addEventListener(BlogErrorEvent.DELETE_FAVORITES_ERROR, deleteFavoriteError, false, 0, true);
		}
		
		
		private function deleteFavoriteResult(e:BlogResultEvent):void
		{
			UserBlog.getInstance().removeEventListener(BlogResultEvent.DELETE_FAVORITES_RESULT, deleteFavoriteResult);
			UserBlog.getInstance().removeEventListener(BlogErrorEvent.DELETE_FAVORITES_ERROR, deleteFavoriteError);
			
			IVisualElementContainer(parent).removeElement(this);
		}
		
		
		private function deleteFavoriteError(e:BlogErrorEvent):void
		{
			WindowUtil.toolTip('删除收藏失败');
			UserBlog.getInstance().removeEventListener(BlogResultEvent.DELETE_FAVORITES_RESULT, deleteFavoriteResult);
			UserBlog.getInstance().removeEventListener(BlogErrorEvent.DELETE_FAVORITES_ERROR, deleteFavoriteError);
		}
		
		
		private function commentHandler(e:ContextMenuEvent):void
		{
			WindowUtil.getInstance().openCommentWindow(data);
		}
		
		
		private function repostHandler(e:ContextMenuEvent):void
		{
			WindowUtil.getInstance().openRepostWindow(data);
		}
		
		
		/**Delete*/
		private function delHandler(e:ContextMenuEvent):void
		{
			UserBlog.getInstance().delete_Status(data.id);
			UserBlog.getInstance().addEventListener(BlogResultEvent.DELETE_STATUS_RESULT, deleteStatusResult, false, 0, true);
			UserBlog.getInstance().addEventListener(BlogErrorEvent.DELETE_STATUS_ERROR, deleteStatusError, false, 0, true);
		}
		
		
		private function deleteStatusResult(e:BlogResultEvent):void
		{
			UserBlog.getInstance().removeEventListener(BlogResultEvent.DELETE_STATUS_RESULT, deleteStatusResult);
			UserBlog.getInstance().removeEventListener(BlogErrorEvent.DELETE_STATUS_ERROR, deleteStatusError);
			
			(parent as IVisualElementContainer).removeElement(this);
		}
		
		
		private function deleteStatusError(e:BlogErrorEvent):void
		{
			UserBlog.getInstance().removeEventListener(BlogResultEvent.DELETE_STATUS_RESULT, deleteStatusResult);
			UserBlog.getInstance().removeEventListener(BlogErrorEvent.DELETE_STATUS_ERROR, deleteStatusError);
		}
	}
}
				